FUNCTION YBC_SWS_CALLBACK_SO.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_APPLEVEL) TYPE  YBC_SWS_APPROVER-APPLEVEL
*"     VALUE(IV_BUSINESSID) TYPE  YBC_SWS_REQUEST-BUSINESSID
*"     VALUE(IV_ACTION) TYPE  YBC_SWS_APPROVER-ACTION
*"     VALUE(IV_COMMENTS) TYPE  YBC_SWS_APPROVER-COMMENTS OPTIONAL
*"     VALUE(IV_SENDBACKLEVEL) TYPE  YBC_SWS_APPROVER-APPLEVEL OPTIONAL
*"     VALUE(IV_APPROVERID) TYPE  YBC_SWS_APPROVER-APPROVERID OPTIONAL
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"     VALUE(EV_FINISHED) TYPE  CHAR1
*"  TABLES
*"      ET_APPROVER_NEXT STRUCTURE  YBCS_SWS_APPROVER OPTIONAL
*"----------------------------------------------------------------------
**********************************************************************
* 若审批人由SwiftSAP配置实现，请勿修改ET_APPROVER_NEXT及EV_FINISHED 内容
* 若审批人由外部函数提供：若是最后一级审批，需设置EV_FINISHED = 'X'
*                        若不是最后一级审批，需提供ET_APPROVER_NEXT
**********************************************************************

  TYPES:
    BEGIN OF TY_TJ30,
      ESTAT TYPE TJ30-ESTAT,
      STONR TYPE TJ30-STONR,
      TXT30 TYPE TJ30T-TXT30,
    END OF TY_TJ30.

  DATA:
    LV_VBELN TYPE VBAK-VBELN,
    LV_OBJNR TYPE VBAK-OBJNR,
    LS_TJ30  TYPE TY_TJ30,
    LT_TJ30  TYPE STANDARD TABLE OF TY_TJ30.

  CLEAR:
    ES_MSG.

  LV_VBELN = IV_BUSINESSID.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      INPUT  = LV_VBELN
    IMPORTING
      OUTPUT = LV_VBELN.

  SELECT SINGLE
    OBJNR
    INTO LV_OBJNR
    FROM VBAK
   WHERE VBELN = LV_VBELN.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    ES_MSG-MESSAGE = TEXT-M01 && LV_VBELN.
    RETURN.
  ENDIF.

  SELECT
    TJ30~ESTAT
    TJ30~STONR
    TJ30T~TXT30
    INTO TABLE LT_TJ30
    FROM TJ30
    JOIN TJ30T
      ON TJ30~STSMA = TJ30T~STSMA
     AND TJ30~ESTAT = TJ30T~ESTAT
   WHERE TJ30~STSMA = 'YSD_SWS'  "须调整成实际配置的值
     AND TJ30T~SPRAS = '1'.

  TRANSLATE IV_ACTION TO UPPER CASE.

  CLEAR LS_TJ30.
  CASE IV_ACTION.
    WHEN 'APPROVE'.
      SORT LT_TJ30 BY STONR.
      DELETE LT_TJ30 WHERE STONR < IV_APPLEVEL.  "该位置显示审批级别配置只能为数字，比如10、20、30.....
      READ TABLE LT_TJ30 INTO LS_TJ30 INDEX 1.

    WHEN 'REJECT'.
      "取最后一条为拒绝 99 已拒绝
      SORT LT_TJ30 BY STONR DESCENDING.
      READ TABLE LT_TJ30 INTO LS_TJ30 INDEX 1.

*    WHEN 'SENDBACK'.
*      CASE IV_SENDBACKLEVEL.
*        WHEN 'RETURN'.
*          SORT LT_TJ30 BY STONR.
*          READ TABLE LT_TJ30 INTO LS_TJ30 INDEX 1.
*          EV_FINISHED = 'X'.
*
*        WHEN OTHERS.
*          SORT LT_TJ30 BY STONR.
*          READ TABLE LT_TJ30 INTO LS_TJ30 WITH KEY STONR = IV_SENDBACKLEVEL.
*      ENDCASE.

    WHEN OTHERS.
  ENDCASE.

  IF LS_TJ30 IS INITIAL.
    ES_MSG-TYPE = 'E'.
    ES_MSG-MESSAGE = TEXT-M02.
    RETURN.
  ELSE.

    CALL FUNCTION 'STATUS_CHANGE_EXTERN'
      EXPORTING
        OBJNR               = LV_OBJNR
        USER_STATUS         = LS_TJ30-ESTAT
        SET_INACT           = ' '
        SET_CHGKZ           = 'X'
      EXCEPTIONS
        OBJECT_NOT_FOUND    = 1
        STATUS_INCONSISTENT = 2
        STATUS_NOT_ALLOWED  = 3
        OTHERS              = 4.

  ENDIF.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.

    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
         INTO ES_MSG-MESSAGE
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    RETURN.
  ENDIF.
ENDFUNCTION.
